package project;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import com.toedter.calendar.*;

import java.text.*;
import javax.swing.JTable.*;
import java.awt.print.*;




public class MainGUI extends JPanel implements ActionListener
{
	private JFrame frame;
	private JPanel topPan, bottomPan, searchResultsPanel;
	private JTextArea bottomJTA;
	private JButton rentB, returnB, bookB;
	
	//top panel components
	private JTextField searchJTF;
	private JComboBox searchJCB;
	private JButton searchB, clearB, addB, otherB;
	private JTextArea topJTA;
	private JScrollPane searchJSP = null;
	
	//Main Panel Changer
	private JPanel changePanel;
	
	//Boxes
	private Box yBox1, yBox2;
	
	//Date Choosers
	private JDateChooser fromDC;
	
	JTable jTable = null;
	
	//bottom half GUI's
	RentGUI rentGUI = null;
	ReturnGUI returnGUI = null;
	BookGUI bookGUI = null;
	
	StockItemDAO dao = new StockItemDAO();
	
	//File chooser
	JFileChooser fileChooser = null;
	
	private String saveSearch = null;
	
	
	
	//CONSTRUCTOR
	//-----------
	public MainGUI() 
	{
		//Text Areas
		//----------
		bottomJTA = new JTextArea();
		bottomJTA.setPreferredSize(new Dimension(400,250));
		bottomJTA.setText("THE BOTTOM!");
		bottomJTA.setBorder(BorderFactory.createEtchedBorder());
		
		//Filler Dimensons
		//----------------
		Dimension xd = new Dimension(5,1);
		Dimension yd = new Dimension(1,5);
		Dimension d1 = new Dimension(1,20);
		Dimension d2 = new Dimension(1,100);
		Dimension d3 = new Dimension(1,Short.MAX_VALUE);
		//----------------------------------------------
		
		ImageIcon rightArrow = new ImageIcon("C:/Users/James/Pictures/LoanTurtle/RightGreenArrow.png");
		ImageIcon searchIcon = new ImageIcon("C:/Users/James/Pictures/LoanTurtle/Search.png");
		
		
		
		
		//Boxes
		//-----
		searchJTF = new JTextField(30);
		searchJTF.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
		searchJTF.addActionListener(this);
		String [] jcbString = new String[]{"Barcode", "Title", "Year", "Status",
										"Date Last Rented"};
		searchJCB = new JComboBox(jcbString);
		searchJCB.setBackground(new Color(238,238,238));
		searchB = new JButton(searchIcon);
		removeButtonStuff(searchB);
		searchB.setBorderPainted(true);
		//searchB.setBackground(new Color(179,238,58));
		searchJCB.setMaximumSize(new Dimension(50,30));
		searchB.addActionListener(this);
		clearB = new JButton("Clear", rightArrow);
		clearB.setHorizontalAlignment(SwingConstants.LEFT);
		clearB.addActionListener(this);
		removeButtonStuff(clearB);
		addB = new JButton("Add", rightArrow);
		addB.addActionListener(this);
		addB.setHorizontalAlignment(SwingConstants.LEFT);
		removeButtonStuff(addB);
		addB.setEnabled(false);
		addB.setPreferredSize(clearB.getPreferredSize());
		addB.setMaximumSize(clearB.getMaximumSize());
		otherB = new JButton("Other", rightArrow);
		otherB.setHorizontalAlignment(SwingConstants.LEFT);
		removeButtonStuff(otherB);
		otherB.setMaximumSize(clearB.getMaximumSize());
		searchJSP = new JScrollPane();
		searchJSP.setBorder(BorderFactory.createEtchedBorder());
		Box xBox2 = new Box(BoxLayout.X_AXIS);
		xBox2.add(new Box.Filler(xd,xd,xd));
		xBox2.add(searchJTF);
		xBox2.add(new Box.Filler(xd,xd,xd));
		xBox2.add(searchJCB);
		xBox2.add(new Box.Filler(xd,xd,xd));
		xBox2.add(searchB);
		xBox2.add(new Box.Filler(xd,xd,xd));
		
		
		
		yBox1 = new Box(BoxLayout.Y_AXIS);
		yBox1.add(clearB);
		yBox1.add(addB);
		//yBox1.add(otherB);
		
		
		
		Box xBox3 = new Box(BoxLayout.X_AXIS);
		xBox3.add(new Box.Filler(xd,xd,xd));
		searchResultsPanel = new JPanel(new GridLayout(1,1));
		searchResultsPanel.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
		searchResultsPanel.setPreferredSize(new Dimension(400,200));
		searchResultsPanel.setBorder(BorderFactory.createTitledBorder("Search Results"));
		searchResultsPanel.add(searchJSP);
		xBox3.add(searchResultsPanel);
		xBox3.add(new Box.Filler(xd,xd,xd));
		xBox3.add(yBox1);
		
		yBox2 = new Box(BoxLayout.Y_AXIS);
		yBox2.setPreferredSize(new Dimension(70,250));
		JLabel fromL = new JLabel("From:");
		yBox2.add(fromL);
		JPanel fromP = new JPanel();
		fromDC = new JDateChooser(new Date());
		fromDC.setMinSelectableDate(new Date());
		fromDC.setEnabled(false);
		//Date theDate = new Date();
		//Date theOtherDate = new Date(theDate.getTime() - 1000000000);
		//fromDC.setDate(theOtherDate);
		java.sql.Date sqlDate = new java.sql.Date(fromDC.getDate().getTime());
		fromDC.setPreferredSize(new Dimension(100,30));
		
		fromP.add(fromDC);
		yBox2.add(fromP);
		yBox2.add(Box.createVerticalStrut(5));
		yBox2.add(new Box.Filler(d1,d2,d3));
		yBox2.setBorder(BorderFactory.createTitledBorder("Dates"));
		
		
		Box yBox3 = new Box(BoxLayout.Y_AXIS);
		yBox3.add(xBox2);
		yBox3.add(Box.createVerticalStrut(5));
		yBox3.add(xBox3);
		
		Box xBox4 = new Box(BoxLayout.X_AXIS);
		xBox4.add(Box.createHorizontalStrut(5));
		xBox4.add(yBox2);
		xBox4.add(yBox3);
		
		
		
		
		
		//ButtonPanel
		rentB = new JButton("RENT");
		rentB.setMaximumSize(new Dimension(Short.MAX_VALUE,40));
		//rentB.setBackground(new Color(245,124,45));
		//rentB.setBorder(BorderFactory.createEtchedBorder());
		removeButtonStuff(rentB);
		//rentB.setBorderPainted(true);
		rentB.addActionListener(this);
		returnB = new JButton("RETURN");
		//returnB.setBackground(new Color(25,200,154));
		//returnB.setBorder(BorderFactory.createEtchedBorder());
		removeButtonStuff(returnB);
		returnB.setBackground(new Color(215,215,215));
		//returnB.setBorderPainted(true);
		returnB.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
		returnB.addActionListener(this);
		bookB = new JButton("BOOK");
		//bookB.setBackground(new Color(125,100,195));
		//bookB.setBorder(BorderFactory.createEtchedBorder());
		removeButtonStuff(bookB);
		bookB.setBackground(new Color(215,215,215));
		//bookB.setBorderPainted(true);
		bookB.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
		bookB.addActionListener(this);
		Box xBox1 = new Box(BoxLayout.LINE_AXIS);
		xBox1.add(rentB);
		xBox1.add(returnB);
		xBox1.add(bookB);
		xBox1.setPreferredSize(new Dimension(Short.MAX_VALUE, 30));
		
		//Add Filler
		//Dimension minSize = new Dimension(50, 20);
		//Dimension prefSize = new Dimension(100, 20);
		//Dimension maxSize = new Dimension(Short.MAX_VALUE, 20);
		//xBox1.add(new Box.Filler(minSize, prefSize, maxSize));
		
		//this JPanel
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(xBox1);
		this.add(new Box.Filler(yd,yd,yd));
		this.add(xBox4);
		this.add(new Box.Filler(yd,yd,yd));
		changePanel = new JPanel(new GridLayout(1,1));
		
			//create all gui's while we're at it!
		rentGUI = new RentGUI(frame, this);
		returnGUI = new ReturnGUI(frame, this);
		bookGUI = new BookGUI(frame, this);
		
		changePanel.add(rentGUI);
		this.add(changePanel);
		this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,rentB.getBackground()));
		//Add Filler
		//Dimension minSize = new Dimension(50, 20);
		//Dimension prefSize = new Dimension(100, 300);
		//Dimension maxSize = new Dimension(20, 100);
		//this.add(new Box.Filler(minSize, prefSize, maxSize));
		
		//frame
		frame = new JFrame("Just a little test...");
		frame.setSize(800,600);
		frame.setMinimumSize(new Dimension(800,600));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(this, BorderLayout.CENTER);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}//-----------------------------end of constructor----------------------------------
	
	public void removeButtonStuff(JButton button)
	{
		button.setBackground(new Color(238,238,238));
		button.setBorderPainted(false);
		button.setFocusPainted(false);
	}
	
	public void resetRentButtonBorder()
	{
		if(rentB.getBorder() == null)
		{
			//rentB.setBorder(BorderFactory.createRaisedBevelBorder());
		}
	}
	public void resetReturnButtonBorder()
	{
		if(returnB.getBorder() == null)
		{
			//returnB.setBorder(BorderFactory.createRaisedBevelBorder());
		}
	}
	public void resetBookButtonBorder()
	{
		if(bookB.getBorder() == null)
		{
			//bookB.setBorder(BorderFactory.createRaisedBevelBorder());
		}
	}
	
	public void clickSearch()
	{
		searchJTF.setText(saveSearch);
		searchB.doClick();
		searchJTF.setText("");
	}
	
	//***************
	//actionPerformed
	//***************
	public void actionPerformed(ActionEvent ae)
	{
		Object source = ae.getSource();
		//SEARCH BUTTON
		//-------------
		if(source == searchB || source == searchJTF)
		{
			saveSearch = searchJTF.getText();
			addB.setEnabled(true);
			searchResultsPanel.removeAll();
			if(searchJCB.getSelectedItem() == "Title")
			{
				jTable = dao.getStockByTitle(searchJTF.getText());
			}
			else if(searchJCB.getSelectedItem() == "Barcode")
			{
				jTable = dao.getStockByBarcode(searchJTF.getText());
			}
			else if(searchJCB.getSelectedItem() == "Year")
			{
				jTable = dao.getStockByYear(searchJTF.getText());
			}
			else if(searchJCB.getSelectedItem() == "Status")
			{
				jTable = dao.getStockByStatus(searchJTF.getText());
			}
			else if(searchJCB.getSelectedItem() == "Date Last Rented")
			{
				jTable = dao.getStockByDLS(searchJTF.getText());
			}
			searchJSP = new JScrollPane(jTable);
			searchJSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			searchResultsPanel.add(searchJSP);
			searchResultsPanel.validate();
		}	
			 
		//-----------------------------------------------------------------------------------
		
		//CLEAR BUTTON
		//------------
		if(source == clearB)
		{
			addB.setEnabled(false);
			searchJTF.setText("");
			searchResultsPanel.removeAll();
			searchResultsPanel.validate();
			searchResultsPanel.repaint();
			//searchResultsPanel.add(searchJSP);
		}//-----------------------------------
		
		//ADD BUTTON
		//----------
		if(source == addB)
		{
			int row = jTable.getSelectedRow();
			if(row == -1)
			{
				JOptionPane.showMessageDialog(this, 
					"You must first select an item");
			}
			else
			{
				String stockId = (String)(jTable.getValueAt(row, 0));
				try
				{
					//Rent
					//----
					if(changePanel.getComponents()[0].getName().compareTo("RentGUI") == 0)
					{
						Object [] rowObject = new Object[]{
							jTable.getValueAt(row, 0),
							jTable.getValueAt(row, 1),
							jTable.getValueAt(row, 11)};
						java.sql.Date tempDate = new java.sql.Date(fromDC.getDate().getTime());
						String date = tempDate.toString();
						
						if(jTable.getValueAt(row, 3).equals("IN"))
						{
							if(dao.checkBookingDate(stockId, date) == false)
							{
								rentGUI.updateCheckOutTable(rowObject);
								rentGUI.updateCheckOutTotal();
							}
							else
							{
								JOptionPane.showMessageDialog(this, "This item is booked out for today.");
							}
						} 
						else if(jTable.getValueAt(row, 3).equals("DAMAGED"))
						{
							int choice;
							choice = JOptionPane.showConfirmDialog(null, "This item has been marked," +
										" 'DAMAGED', \nContinue with rental?", "Damaged item.",
										JOptionPane.YES_NO_OPTION);
							if(choice == JOptionPane.YES_OPTION)
							{
								rentGUI.updateCheckOutTable(rowObject);
								rentGUI.updateCheckOutTotal();
							}
						}
						else
						{
							int choice;
							choice = JOptionPane.showConfirmDialog(null, "This item has not been returned," +
										"\nwould you like to go to RETURN?", "ITEM MARKED \"OUT\"",
										JOptionPane.YES_NO_OPTION);
							if(choice == JOptionPane.YES_OPTION)
							{
								changePanel.removeAll();
								rentB.setBackground(new Color(215,215,215));
								removeButtonStuff(returnB);
								changePanel.add(returnGUI);
								this.validate();
								this.repaint();
							}		
						}
					}
					//Return
					//------
					else if(changePanel.getComponents()[0].getName().compareTo("ReturnGUI") == 0)
					{
						if(jTable.getValueAt(row, 3).equals("OUT"))
						{
							Object [] rowObject = new Object[]{
							jTable.getValueAt(row, 0),
							jTable.getValueAt(row, 1),
							jTable.getValueAt(row, 3),
							jTable.getValueAt(row, 12)};
							returnGUI.updateReturnTable(rowObject);
						}
						else
						{
							JOptionPane.showMessageDialog(this, "This item has already been returned");
						}
						
					}
					//Book
					//----
					else if(changePanel.getComponents()[0].getName().compareTo("BookGUI") == 0)
					{
						
						java.sql.Date tempDate = new java.sql.Date(fromDC.getDate().getTime());
						String date = tempDate.toString();
						java.sql.Date tempToday = new java.sql.Date(new Date().getTime());
						String today = (tempToday.toString());
						System.out.println("date = " + date);
						System.out.println("today = " + today);
						if(jTable.getValueAt(row, 3).equals("OUT") && date.equals(today))
						{
							int choice;
							choice = JOptionPane.showConfirmDialog(null, "This item has not been returned," +
										"\nwould you like to go to RETURN?", "ITEM MARKED \"OUT\"",
										JOptionPane.YES_NO_OPTION);
							if(choice == JOptionPane.YES_OPTION)
							{
								changePanel.removeAll();
								rentB.setBackground(new Color(215,215,215));
								removeButtonStuff(returnB);
								changePanel.add(returnGUI);
								this.validate();
								this.repaint();
							}		
						}
						else if(dao.checkBookingDate(stockId, date) == false)
						{
							Object [] rowObject = new Object[]{
								jTable.getValueAt(row, 0),
								jTable.getValueAt(row, 1),
								jTable.getValueAt(row, 11),
								date};
							bookGUI.updateBookTable(rowObject);
							bookGUI.updateBookTotal();
						}
						else
						{
							JOptionPane.showMessageDialog(this, "This item is booked out for " + date);
						}
					}
				}//end of try block
				catch(NullPointerException npe)
				{
					JOptionPane.showMessageDialog(searchResultsPanel, 
						" You must first do a search and \nselect and item before adding. ");
					npe.printStackTrace();
				}
				catch(IndexOutOfBoundsException ioe)
				{
					JOptionPane.showMessageDialog(searchResultsPanel, " No item selected. ");
					ioe.printStackTrace();
				}
			}
			
		}//------------------------------------------------------------------------------
		
		
		//RENT BUTTON
		//-----------
		if(source == rentB)
		{
			if(changePanel.getComponents()[0].getName().compareTo("RentGUI") != 0)
			{
				fromDC.setEnabled(false);
				fromDC.setDate(new Date());
				bookB.setBackground(new Color(215,215,215));
				returnB.setBackground(new Color(215,215,215));
				removeButtonStuff(rentB);
				this.validate();
				this.repaint();
				bookGUI.removeRows();
				returnGUI.removeRows();
				changePanel.removeAll();
				changePanel.add(rentGUI);
				changePanel.validate();
				this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,rentB.getBackground()));
				rentB.setBorder(null);
				resetReturnButtonBorder();
				resetBookButtonBorder();
			}
		}//------------------------------------------------------------------------------------
		
		//RETURN BUTTON
		//-------------
		if(source == returnB)
		{
			if(changePanel.getComponents()[0].getName().compareTo("ReturnGUI") != 0)
			{
				fromDC.setEnabled(false);
				fromDC.setDate(new Date());
				bookB.setBackground(new Color(215,215,215));
				rentB.setBackground(new Color(215,215,215));
				removeButtonStuff(returnB);
				rentGUI.removeRows();
				bookGUI.removeRows();
				changePanel.removeAll();
				changePanel.add(returnGUI);
				changePanel.validate();
				this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,returnB.getBackground()));
				//returnB.setBorder(null);
				resetRentButtonBorder();
				resetBookButtonBorder();
			}
		}//-------------------------------------------------------------------------------------
		
		//BOOK BUTTON
		//-----------
		if(source == bookB)
		{
			if(changePanel.getComponents()[0].getName().compareTo("BookGUI") != 0)
			{
				fromDC.setEnabled(true);
				rentB.setBackground(new Color(215,215,215));
				returnB.setBackground(new Color(215,215,215));
				removeButtonStuff(bookB);
				rentGUI.removeRows();
				returnGUI.removeRows();
				changePanel.removeAll();
				changePanel.add(bookGUI);
				changePanel.validate();
				this.setBorder(BorderFactory.createMatteBorder(6,6,6,6,bookB.getBackground()));
				resetRentButtonBorder();
				resetReturnButtonBorder();
			}
		}//--------------------------------------------------------------------------------------
	}
	
	
	public static void main(String[] args) 
	{
		new MainGUI();
	}	
}
